<?php
namespace Common\Model;
use Common\Model\BaseModel;
/**
* 第三方登录model
*/
class UserModel extends BaseModel{
    // 自动验证
    protected $_validate=array(
        array('type','require','类型必填'),
        array('username','require','昵称必填'),
        array('password','require','密码必填'),
        array('openid','require','openid必填'),
        array('email','require','邮箱必填'),
        array('access_token','require','access_token必填'),
        );

    // 自动完成
    protected $_auto=array(
        array('create_time','time',1,'function'),
        array('last_login_time','time',3,'function'),
        array('last_login_ip','getLoginIp',3,'callback'),
        array('login_times',1),
        array('status',1),
        );

    // 获得登录ip
    function getLoginIp(){
        $ip=get_client_ip();
        return $ip;
    }

    // 添加数据
    public function addData($data){
        if($create=$this->create($data)){
            $id=$this->add($create);
            return $id;
        }
    }

    // 修改数据
    public function editData($data){
        $uid=$data['uid'];
        if($create=$this->create($data)){
            $create['login_times']=array('exp','login_times+1');
            unset($create['status']);
            unset($create['create_time']);
            $this->where(array('uid'=>$uid))->save($create);
            return $this->getFieldByUid($uid,'uid');
        }
    }

    // 传递uid获取单条数据
    public function getDataByUid($uid){
        return $this->where(array('uid'=>$uid))->find();
    }

    // 获取用户分页数据
    public function getPageData(){
        $count=$this->count();
        $page=new \Org\Bjy\Page($count,20);
        $list=$this
            ->order('last_login_time desc')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $type=array('','QQ','新浪微博','豆瓣','人人','开心网');
        foreach ($list as $k => $v) {
            $list[$k]['type']=$type[$v['type']];
        }
        $data=array(
            'data'=>$list,
            'page'=>$page->show()
            );
        return $data;

    }

    public function insert($email, $password, $username){


        $salt     = rand_string( 10 );
        $password = sha1( md5( $password ) .  $salt );
        $data  = array(
            'username'  => $username,
            'password'  => $password,
            'email'     => $email,
            'salt'      => $salt,
            'create_time'=>time()
        );
        $result = $this->add($data);
        if ($result) {

            return $result;
        }else{
            return false;
        }
    }


    public  function  autoLogin($user, $admin=false) {
        $data = array(
            'last_login_time' => time(),
            'last_login_ip'=>$this->getLoginIp(),
        );
        $this->where(array('uid'=>$user['uid']))->save($data);

        $auth = array(
            'uid'             => $user['uid'],
            'username'        => $user['username'],
            'last_login_time' => $user['last_login_time'],
            'head_img'=>avatar($user['uid'])
        );

        session('user', $auth);
        session('user_sign', data_auth_sign($auth));
        if ($admin) {
            session('is_admin', true);
        }

    }


    public  function login($email, $password) {
        $user = $this->where( array( 'email'=>$email ) )->find();
        $password  = sha1( md5( $password ) . $user['salt'] );
        if (!$user) {
            $this->error = '用户不存在！';
            return false;
        }

        if ($user['status']==0) {
            $this->error = '您被禁止登录了！';
            return false;
        }
        if($user['password'] !== $password ){

            $this->error = '密码错误';
            return false;
        }


        $this->autoLogin($user, $admin ? true : false);

        return  $user;
    }


    public  function setPassword($uid, $password){
        $salt     =  rand_string( 10 );
        $password =  sha1( md5( $password ) . $salt );
        $result = $this->where(array('uid'=>$uid))->save(array('password'=>$password, 'salt'=>$salt));
        if ($result) {
            return  true;
        } else {
            return  false;
        }
    }


}
